<?php

namespace App\Notifications\Reservation;

use App\Models\Reservation\Reservation;
use App\User;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Notifications\Notification;

class CancelReservationToDoctor extends Notification implements ShouldQueue
{
    use Queueable;

    /**
     * @var \App\Models\Reservation\Reservation | \Illuminate\Database\Eloquent\Collection
     */
    protected $reservation;

    public function __construct($reservation)
    {
        $this->reservation = $reservation;
    }

    public function via($notifiable)
    {
        return ['database'];
    }

    /**
     * Get the array representation of the notification.
     * @param  User $notifiable
     * @return array
     */
    public function toDatabase($notifiable)
    {
        $data = [];
        if ($this->reservation instanceof Collection) {
            if (1 == $this->reservation->count()) {
                $data = $this->singleMessage($this->reservation->first());
            } else {
                $data = $this->multipleMessage();
            }
        }

        if ($this->reservation instanceof Reservation) {
            $data = $this->singleMessage($this->reservation);
        }

        return $data;
    }

    protected function multipleMessage()
    {
        $data['type']    = 'multiple_cancel_reservation';
        $data['source']  = '系统消息';
        $data['count']   = $this->reservation->count();
        $data['message'] = sprintf('您取消了 %d位患者的预约。', $this->reservation->count());

        $detail         = $this->reservation->map(function (Reservation $reservation) {
            return $this->format($reservation);
        });
        $data['detail'] = $detail->toArray();

        return $data;
    }

    protected function singleMessage($reservation)
    {
        $data['type']   = 'cancel_reservation';
        $data['source'] = '系统消息';
        $data['count']  = 1;

        if (\Auth::user()->isA('nurse')) {
            $data['message'] = sprintf(
                '您取消了 %s %s %s 的出诊(患者：%s)。',
                $reservation->doctor->name,
                $reservation->time,
                $reservation->am ? '上午' : '下午',
                $reservation->patient->name
            );
        } else {
            $data['message'] = sprintf(
                '您取消了%s %s 的出诊(患者：%s)。',
                $reservation->time,
                $reservation->am ? '上午' : '下午',
                $reservation->patient->name
            );
        }

        $data['detail'] = $this->format($reservation);

        return $data;
    }

    protected function format(Reservation $reservation)
    {
        $time = Carbon::parse($reservation->time)->startOfDay();

        return [
            'id'           => $reservation->id,   //  预约ID
            'patient_id'   => $reservation->patient_id,
            'patient_name' => $reservation->patient->name,
            'time'         => $reservation->time .
                ' ' . $time->hour($reservation->start)->format('H:i') .
                '-' . $time->hour($reservation->end)->format('H:i'),
            'am'           => $reservation->am ? '上午' : '下午',
            'room'         => $reservation->room,
        ];
    }
}
